package kr.co.exight.onApp;

import library.Library;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class ActivityCardList extends Activity {

	/** Called when the activity is first created. */


	public static JSONObject data;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//arrCompanyInfo = ActivityTabOncard.getArrayData();
		setContentView(R.layout.act_cardlist);

		ListView lstView = (ListView)findViewById(R.id.card_lstList);
		TestAdapter adapter = new TestAdapter();

		lstView.setAdapter(adapter);
		// TODO Auto-generated method stub
	}

	public static void setJSONData(JSONObject _data){
		data = _data;
	}


	class TestAdapter extends BaseAdapter{

		public int getCount() {

			try {
				return data.getJSONArray("spots").length();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return 0;
		}

		public Object getItem(int position) {

			try {
				return data.getJSONArray("spots").get(position);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return position;
		}

		public long getItemId(int position) {

			return 0;
		}

		public View getView(final int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if(v == null){
				v = getLayoutInflater().inflate(R.layout.cell_cardlist, null);
			}

			final JSONObject a;
			try {
				a = data.getJSONArray("spots").getJSONObject(position);

				TextView tvCategory = (TextView)v.findViewById(R.id.cardCell_tvCategory);
				TextView tvName = (TextView)v.findViewById(R.id.cardCell_tvName);
				LinearLayout llList = (LinearLayout)v.findViewById(R.id.cardCell_ll);

				Library.setFontNanumGothic(tvCategory);
				Library.setFontNanumGothic(tvName);
				Library.setTextViewBold(tvCategory);
				Library.setTextViewBold(tvName);
				
				tvCategory.setText("제휴점");
				tvName.setText(a.getString("name"));
				
				llList.setOnClickListener(new OnClickListener() {

					public void onClick(View v) {
						// TODO Auto-generated method stub
						ActivityCardDetail.setJSONData(a);
						Library.startActivity(ActivityCardList.this, ActivityCardDetail.class);
					}
				});
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return v;
		}
	}

}
